<?php
// +----------------------------------------------------------------------
// | RXThinkCMF_EVTP6_PRO前后端分离旗舰版框架 [ RXThinkCMF ]
// +----------------------------------------------------------------------
// | 版权所有 2021 南京RXThinkCMF研发中心
// +----------------------------------------------------------------------
// | 官方网站: http://www.rxthink.cn
// +----------------------------------------------------------------------
// | 作者: 牧羊人 <rxthinkcmf@163.com>
// +----------------------------------------------------------------------
// | 免责声明:
// | 本软件框架禁止任何单位和个人用于任何违法、侵害他人合法利益等恶意的行为，禁止用于任何违
// | 反我国法律法规的一切平台研发，任何单位和个人使用本软件框架用于产品研发而产生的任何意外
// | 、疏忽、合约毁坏、诽谤、版权或知识产权侵犯及其造成的损失 (包括但不限于直接、间接、附带
// | 或衍生的损失等)，本团队不承担任何法律责任。本软件框架只能用于公司和个人内部的法律所允
// | 许的合法合规的软件产品研发，详细声明内容请阅读《框架免责声明》附件；
// +----------------------------------------------------------------------

namespace app\adminmini\service;


use app\adminmini\model\Door;
use think\facade\Db;

/**
 * 门禁列管理-服务类
 * @author 牧羊人
 * @since: 2022/01/23
 * Class DoorListService
 * @package app\admincustomer\service
 */
class DoorlistService extends BaseService
{
    /**
     * 构造函数
     * LevelService constructor.
     */
    public function __construct()
    {
        $this->model = new Door();
    }

    /**
     * 获取数据列表
     * @return array
     * @since 2022/01/23
     * @author 牧羊人
     */
    public function getList()
    {
        $param = request()->param();

        // 查询条件
        $map = [];


        // 设备名称
        $name = isset($param['name']) ? trim($param['name']) : '';
        if ($name) {
            $map[] = ['name', 'like', "%{$name}%"];
        }

        $token = request()->header("Authorization");
        if ($token && strpos($token, 'Bearer ') !== false) {
            $token = str_replace("Bearer ", null, $token);
            // JWT解密token
            $jwt = new \Jwt();
            $this->userId = $jwt->verifyToken($token);
        }


        $map[] = ['store_id', '=', $this->userId()];
        $list = Db::name('customer_door_list')->where('mark', 1)->where($map)->page(PAGE, PERPAGE)->select();
        foreach ($list as $i => $v) {
            $v['pid_name'] = '';
            if ($v['pid'] > 0) {
                $info = Db::name('customer_door_list')->where('id', $v['pid'])->find();
                $v['pid_name'] = $info['name'];
            }

            $list[$i] = $v;
        }
        $count = Db::name('customer_door_list')->where('mark', 1)->where($map)->page(PAGE, PERPAGE)->count();


        //返回结果
        $message = array(
            "msg" => '操作成功',
            "code" => 0,
            "data" => $list,
            "count" => $count,
        );
        return $message;
    }

    public function edit()
    {

        $data = request()->param();
        $token = request()->header("Authorization");
        if ($token && strpos($token, 'Bearer ') !== false) {
            $token = str_replace("Bearer ", null, $token);
            // JWT解密token
            $jwt = new \Jwt();
            $this->userId = $jwt->verifyToken($token);
        }


        $data['store_id'] = $this->userId();
        if ($data['is_door'] == 1) {
            // 记录保洁权限
            $info = Db::name('customer_order_cleaning')->where('door_id', $data['door_id'])->find();
            if(!$info){

                $datac['add_time'] = time();
                $datac['store_id'] = $this->userId();
                $datac['end_time'] = 0;
                $datac['door_id'] = $data['door_id'];
                Db::name('customer_order_cleaning')->insert($datac);
            }else{
                $datac['door_id'] = $data['door_id'];
                Db::name('customer_order_cleaning')->where('id',$info['id'])->update($datac);
            }

        }
        return parent::edit($data); // TODO: Change the autogenerated stub
    }

}